package systems;

import general.Case;
import general.Mechanisme;
import general.Source;
import graphique.C;

import java.util.ArrayList;

public class FluidSystem
{
	public ArrayList<Case> waterCases = new ArrayList<Case>();
	Mechanisme m;
	
	
	
	public FluidSystem(Mechanisme m)
	{
		this.m = m;
	}

	public void avancer()
	{
		ArrayList<Case> newWaterCases = new ArrayList<Case>();
		ArrayList<Case> localWaterCases = new ArrayList<Case>();
		double qw = 0;
		
		
		for( int i = 0 ; i < waterCases.size() ; i++ )
		{
			localWaterCases.clear();
			
			if( waterCases.get(i).eau > C.minLiquide )
			{
				for( int j = 0 ; j < waterCases.get(i).asc.size() ; j++ )
				{
					if( waterCases.get(i).asc.get(j).eau + waterCases.get(i).asc.get(j).zone.p1.alt < waterCases.get(i).eau + waterCases.get(i).zone.p1.alt )
					{
						if( liquideEffect(  waterCases.get(i) , waterCases.get(i).asc.get(j) ) )
							localWaterCases.add(waterCases.get(i).asc.get(j));
					}
				}
				
				qw = waterCases.get(i).eau / localWaterCases.size();
				
				waterCases.get(i).eau = 0;
				waterCases.get(i).source = null;
				//System.out.println( waterCases.get(i).asc.size() );
				
				for( int j = 0 ; j < localWaterCases.size() ; j++ )
				{
					//System.out.println("A : " + j + " " + waterCases.get(i).asc.get(j) );
					localWaterCases.get(j).eau += qw;
					localWaterCases.get(j).type = waterCases.get(i).type;
					
					liquideConsequence( localWaterCases.get(j) );
					
					if( !newWaterCases.contains( localWaterCases.get(j) ) )
					{
						newWaterCases.add( localWaterCases.get(j) );
					}
				}
			}
			else
			{
				waterCases.get(i).eau = 0;
			}
		}
		
		waterCases = newWaterCases;
		
		//System.out.println("New Size : " + newWaterCases.size());
		
		

		
	}

	private void liquideConsequence( Case case1 )
	{
		if( case1.type == 1 || case1.type == 2 )
		{
			//ps.aa.removeAll( case1.la );
			//case1.la.clear();
			
			for( int i = 0 ; i < case1.la.size() ; i++ )
			{
				if( case1.la.get(i).mode == 0 )
				{
					case1.la.get(i).mode = 1;
					case1.la.get(i).age = 0;
				}
				else if( case1.la.get(i).mode == 2 )
				{
					m.ps.aa.remove( case1.la.get(i) );
					case1.la.remove( case1.la.get(i) );
				}
			}
		}	
	}

	private boolean liquideEffect(Case case1, Case case2)
	{
		if( case1.type != case2.type && case2.type != 0 )
			return false;
		
		if( case1.type == 2 && case1.zone.p1.alt < C.borneWater )
			return false;
		
		return true;
	}

	public void source(Case cible , double d, int i , int idF )
	{
		if( !waterCases.contains( cible ) )
		{
			waterCases.add( cible );
		}
		
		cible.eau = d;
		cible.type = i;
		cible.source = new Source(cible);
		cible.idF = idF;
	}

	public ArrayList<Case> clusturing(Case lieu)
	{
		ArrayList<Case> s = new ArrayList<Case>();
		
		for( int i = 0 ; i < waterCases.size() ; i++ )
		{
			if( waterCases.get(i).idF == lieu.idF )
			{
				s.add( waterCases.get(i) );
			}
		}
		
		return s;
	}
}
